package org.algorithm.chessboard;


import java.awt.*;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.concurrent.CountDownLatch;

/**
 * 马踏棋盘
 */
public class HorseChessBoard {
    //棋盘列数
    private static int X;
    //棋盘行数
    private static int Y;
    //棋盘访问记录
    private static boolean visited[];
    //完成记录
    private static boolean finished;

    public static void main(String[] args) {
        System.out.println("骑士周游算法，开始运行~~");
        //测试骑士周游算法是否正确
        X = 12;
        Y = 12;
        int row = 1; //马儿初始位置的行，从1开始编号
        int column = 1; //马儿初始位置的列，从1开始编号
        //创建棋盘
        int[][] chessboard = new int[X][Y];
        visited = new boolean[X * Y];//初始值都是false
        //测试一下耗时
        long start = System.currentTimeMillis();
        traversalChessboard(chessboard, row - 1, column - 1, 1);
        long end = System.currentTimeMillis();
        System.out.println("共耗时: " + (end - start) + " 毫秒");

        //输出棋盘的最后情况
        for (int[] rows : chessboard) {
            for (int step : rows) {
                System.out.print(step + "\t");
            }
            System.out.println();
        }
    }

    public static void traversalChessboard(int[][] chessBoard, int column, int row, int step) {
        visited[row * X + column] = true;
        chessBoard[row][column] = step;

        ArrayList<Point> ps = next(new Point(column, row));
        ps.sort(new Comparator<Point>() {
            @Override
            public int compare(Point o1, Point o2) {
                int s1 = next(o1).size();
                int s2 = next(o2).size();
                return s1 - s2;
            }
        });
        while (!ps.isEmpty()) {
            Point point = ps.remove(0);
            if (!visited[point.y * X + point.x]) {
                traversalChessboard(chessBoard, point.x, point.y, step + 1);
            }
        }
        if (step < X * Y && !finished) {
            visited[row * X + column] = false;
            chessBoard[row][column] = 0;
        } else {
            finished = true;
        }
    }


    public static ArrayList<Point> next(Point curPoint) {
        ArrayList<Point> ps = new ArrayList<>();
        Point p1 = new Point();
        /**
         * 按照当前传入的point为中间位置，分别计算马可以走的所有位置
         */

        //跳到5这个位置的时候
        if ((p1.x = curPoint.x - 2) >= 0 && (p1.y = curPoint.y - 1) >= 0) {
            ps.add(new Point(p1));
        }
        //跳到6这个位置的时候
        if ((p1.x = curPoint.x - 1) >= 0 && (p1.y = curPoint.y - 2) >= 0) {
            ps.add(new Point(p1));
        }
        //跳到7这个位置的时候
        if ((p1.x = curPoint.x + 1) < X && (p1.y = curPoint.y - 2) >= 0) {
            ps.add(new Point(p1));
        }
        //跳到0这个位置的时候
        if ((p1.x = curPoint.x + 2) < X && (p1.y = curPoint.y - 1) >= 0) {
            ps.add(new Point(p1));
        }
        //跳到1这个位置的时候
        if ((p1.x = curPoint.x + 2) < X && (p1.y = curPoint.y + 1) < Y) {
            ps.add(new Point(p1));
        }
        //跳到2这个位置的时候
        if ((p1.x = curPoint.x + 1) < X && (p1.y = curPoint.y + 2) < Y) {
            ps.add(new Point(p1));
        }
        //跳到3这个位置的时候
        if ((p1.x = curPoint.x - 1) >= 0 && (p1.y = curPoint.y + 2) < Y) {
            ps.add(new Point(p1));
        }
        //跳到4这个位置的时候
        if ((p1.x = curPoint.x - 2) >= 0 && (p1.y = curPoint.y + 1) < Y) {
            ps.add(new Point(p1));
        }
        return ps;
    }
}
